package jp.co.hcs.attendancemanagement.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;

/**
 * The Class DateUtil.
 */
public class DateUtil {

  /** The day of week map. */
  public static HashMap<String, String> dayOfWeekMap;

  static {
    dayOfWeekMap = new HashMap<String, String>();
    dayOfWeekMap.put("1", "Sunday");
    dayOfWeekMap.put("2", "Monday");
    dayOfWeekMap.put("3", "Tuesday");
    dayOfWeekMap.put("4", "Wednesday");
    dayOfWeekMap.put("5", "Thursday");
    dayOfWeekMap.put("6", "Friday");
    dayOfWeekMap.put("7", "Saturday");
  }

  /**
   * Gets the end of year.
   * 
   * @param startDate the start date
   * @return the end of year
   */
  public static Date getEndOfYear(Date startDate) {
    Calendar cal = GregorianCalendar.getInstance();
    cal.setTime(startDate);
    cal.add(Calendar.YEAR, 1);
    cal.add(Calendar.DATE, -1);
    return cal.getTime();
  }

  /**
   * Gets the hours.
   * 
   * @param date the date
   * @return the hours
   */
  public static int getHours(Date date) {
    Calendar cal = GregorianCalendar.getInstance();
    cal.setTime(date);
    return cal.get(Calendar.HOUR);
  }

  /**
   * Gets the minutes.
   * 
   * @param date the date
   * @return the minutes
   */
  public static int getMinutes(Date date) {
    Calendar cal = GregorianCalendar.getInstance();
    cal.setTime(date);
    return cal.get(Calendar.MINUTE);
  }

  /**
   * Convert date to hour format.
   * 
   * @param date the date
   * @return the string
   */
  public static String convertDateToHourFormat(Date date) {
    if (date == null) {
      return null;
    }
    SimpleDateFormat df = new SimpleDateFormat("HH:mm");
    return df.format(date);
  }

  /**
   * Convert date.
   * 
   * @param date the date
   * @return the string
   */
  public static String convertDate(Date date) {
    if (date == null) {
      return null;
    }
    SimpleDateFormat df = new SimpleDateFormat("MM/dd");
    return df.format(date);
  }

  /**
   * Adds the some days.
   * 
   * @param monthDay the month day
   * @param year the year
   * @param days the days
   * @return the string
   */
  public static String addSomeDays(String monthDay, String year, int days) {
    SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy");
    Calendar cal = GregorianCalendar.getInstance();
    try {
      Date date = df.parse(monthDay + "/" + year);
      cal.setTime(date);
    } catch (ParseException e) {
      // do nothing
    }
    cal.add(Calendar.DATE, days);
    df = new SimpleDateFormat("MM/dd");
    return df.format(cal.getTime());
  }

  /**
   * Checks if is before.
   * 
   * @param date1 the date1
   * @param date2 the date2
   * @return true, if is before
   */
  public static boolean isBefore(String date1, String date2) {
    if (StringUtil.isEmpty(date1) || StringUtil.isEmpty(date2)) {
      return false;
    }

    String[] dateSpliter;

    dateSpliter = date1.split("/");
    int month1 = Integer.parseInt(dateSpliter[0]);
    int day1 = Integer.parseInt(dateSpliter[1]);

    dateSpliter = date2.split("/");
    int month2 = Integer.parseInt(dateSpliter[0]);
    int day2 = Integer.parseInt(dateSpliter[1]);

    if (month1 < month2) {
      return true;
    } else if (month1 > month2) {
      return false;
    } else {
      if (day1 < day2) {
        return true;
      } else {
        return false;
      }
    }
  }

  /**
   * Parses the date.
   * 
   * @param date the date
   * @param year the year
   * @return the date
   */
  public static Date parseDate(String date, String year) {
    SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy");
    try {
      return df.parse(date + "/" + year);
    } catch (ParseException e) {
      return new Date();
    }
  }

  /**
   * Parses the date from h hmm.
   * 
   * @param time the time
   * @return the date
   */
  public static Date parseDateFromHHmm(String time) {
    if (StringUtil.isEmpty(time)) {
      time = "00:00";
    }
    SimpleDateFormat df = new SimpleDateFormat("HH:mm");
    try {
      return df.parse(time);
    } catch (ParseException e) {
      return new Date();
    }
  }
  
  /**
   * Parses the date.
   * 
   * @param date the date
   * @param year the year
   * @return the date
   */
  public static Date parseDateMMdd(String date, String year) {
    SimpleDateFormat df = new SimpleDateFormat("MMddyyyy");
    try {
      return df.parse(date + year);
    } catch (ParseException e) {
      return new Date();
    }
  }

}
